package be.kdg.componenten.client;

import be.kdg.componenten.chatters.Chatter;
import be.kdg.componenten.chatters.ChatterImpl;
import be.kdg.componenten.chatters.Chatters;
import be.kdg.componenten.chatters.TextReceiver;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.rmi.RemoteException;
import java.util.List;
import java.io.Serializable;

/**
 * Tom Verelst
 * Wim Goeman
 */
public class ChatClientFrame extends JFrame implements TextReceiver, Serializable {

	private String naam;
	private JTextArea messages;
	private JTextField messageToSend;
	private JButton sendButton;
	private JButton exitButton;
	private JLabel lblNaam;
	private ChatterImpl chatter;
	private DefaultListModel listModel;
	private JList chattersList;

	public ChatClientFrame(String naam, ChatterImpl chatter) {
		super("Chat Client");
		this.chatter = chatter;
		createComponents();
		createListeners();
		this.naam = naam;
		setNaam(naam);
		setSize(450, 360);
		setVisible(true);

		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				exitApp();
			}
		});
	}

	private void exitApp(){
		try{
			chatter.unregister(chatter);
		}catch(RemoteException re){
			JOptionPane.showMessageDialog(this, "Error while shutting down program. System exit code 1!");
			dispose();
			System.exit(1);
		}
		dispose();
		System.exit(0);
	}

	public void receive(String text) {
		messages.setText(messages.getText() + text + "\n");
	}

	private void setNaam(String naam) {
		this.naam = naam;
		lblNaam.setText(naam);
	}

	private void createComponents() {
		Container pane = getContentPane();
		pane.setLayout(new BorderLayout());

		JPanel northPanel = new JPanel(new FlowLayout());
		lblNaam = new JLabel();
		exitButton = new JButton("Exit");
		northPanel.add(lblNaam);
		northPanel.add(exitButton);

		messages = new JTextArea();
		messages.setEditable(false);
		messages.setLineWrap(true);
		messages.setWrapStyleWord(true);

		listModel = new DefaultListModel();
		chattersList = new JList(listModel);

		JSplitPane centerPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(messages), new JScrollPane(chattersList));
		centerPanel.setResizeWeight(1);
		centerPanel.setDividerLocation(300);
		centerPanel.setDividerSize(2);

		JPanel southPanel = new JPanel(new FlowLayout());
		messageToSend = new JTextField(25);
		sendButton = new JButton("Send");
		southPanel.add(messageToSend);
		southPanel.add(sendButton);

		pane.add(northPanel, BorderLayout.NORTH);
		pane.add(centerPanel, BorderLayout.CENTER);
		pane.add(southPanel, BorderLayout.SOUTH);
	}

	private void createListeners() {
		sendButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendMessage();
			}
		});

		messageToSend.addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent e) {
				int key = e.getKeyCode();
				if (key == KeyEvent.VK_ENTER) {
					sendMessage();
				}
			}

			public void keyReleased(KeyEvent e) {
			}

			public void keyTyped(KeyEvent e) {
			}
		});

		exitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				exitApp();
			}
		});
	}

	private void sendMessage() {
		String message = messageToSend.getText();
		if (!message.trim().equals("")) {
			try{
				chatter.send(message);
			}catch( RemoteException re){
				re.printStackTrace();
			}

		} else {
			System.out.println("Ignored message: empty");
		}
		messageToSend.setText("");
		messageToSend.requestFocus();
	}

	public void updateChatters(Chatters chatters) throws RemoteException {
		listModel.removeAllElements();
		List<Chatter> chattersList = chatters.getChatters();
		for(Chatter chatter : chattersList){
			listModel.addElement(chatter.getName());
		}

	}


}
